package com.dongdongshop.controller;

import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.constant.HttpResultEnum;
import com.dongdongshop.data.DataResult;
import com.dongdongshop.pojo.evaluate.Evaluate;
import com.dongdongshop.util.HttpClientUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Author Eayon
 * @Date 2020/3/20 22:37
 * 还有一部分评论功能在UserHomeController类中
 */
@Controller
@RequestMapping("evaluate")
@CrossOrigin("*")//跨域注解
public class EvaluateController {

    private static final String EVALUATE_URL = "http://localhost:8765/evaluateservice/evaluate/";

    /**
     * 调用评论服务  通过goodsid查询所有评论
     * @param goodsId
     */
    @RequestMapping("getEvaluatesByGoodsId")
    @ResponseBody
    public DataResult getEvaluatesByGoodsId(Long goodsId){
        //调用评论服务查询该商品下的所有评论
        Map<String,String> params = new HashMap<>();
        String dataResultStr = HttpClientUtil.doGet(EVALUATE_URL + goodsId, params);
        DataResult<List<Evaluate>> dataResult = JSONObject.parseObject(dataResultStr, DataResult.class);
        List<Evaluate> evaluates = dataResult.getData();
        if(evaluates.size() <= 0){
            //表示该商品没有评论
            return new DataResult(HttpResultEnum.ERROR.getCode(), HttpResultEnum.ERROR.getMessage());
        }
        return new DataResult(HttpResultEnum.SUCCESS.getCode(), HttpResultEnum.SUCCESS.getMessage());
    }

    /**
     * 评论点赞
     * @param id
     * @return
     */
    @RequestMapping("incLikeNum")
    @ResponseBody
    public DataResult incLikeNum(Integer id) throws IOException {
        //调用评论服务查询该商品下的所有评论
        Map<String,String> params = new HashMap<>();
        String dataResultStr = HttpClientUtil.doPost(EVALUATE_URL + id, params);
        DataResult dataResult = JSONObject.parseObject(dataResultStr, DataResult.class);
        return dataResult;
    }


}
